package com.speechb.modules.system.admin.controller;

import com.alibaba.fastjson.JSON;
import com.speechb.modules.system.admin.controller.base.BaseController;
import com.speechb.modules.system.admin.service.ISysUserService;
import com.speechb.modules.utils.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : llj
 * @version :2.1.0
 * @date : 2019/10/3 8:06
 */
@Controller
@RequestMapping("${adminRoot}")
public class LoginController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    ISysUserService sysUserService;

    @RequestMapping("/login")
    public String login(String username,String password) {
         return "admin/login";
    }
    @RequestMapping("/login_check")
    @ResponseBody
    public void loginCheck(String username,String password) {
        logger.info(JSON.toJSONString(sysUserService.list()));
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            UserUtil.getSubject().login(token);

            renderSuccess();
        } catch (AuthenticationException e) {
            logger.warn("login fail", e);
            renderFail();
        }
    }
}
